import java.util.*;

/**
 * @author LKQ
 * @date 2022/6/16 10:20
 * @description 排序 + 双指针
 */
public class Solution4 {
    public static void main(String[] args) {

    }
    public int findPairs(int[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length, y = 0, res = 0;
        for (int x = 0; x < n; x++) {
            if (x == 0 || nums[x] != nums[x - 1]) {
                while (y < n && (nums[y] < nums[x] + k || y <= x)) {
                    y++;
                }
                if (y < n && nums[y] == nums[x] + k) {
                    res++;
                }
            }
        }
        return res;
    }
}
